package teacher.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import teacher.bean.Paper;
import teacher.service.paper;
import tools.StringDate;

import com.opensymphony.xwork2.ActionSupport;

public class modifyPap2 extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private File fileCom;
	private String fileComFileName;
	private String uploadPath;
	private String FilePath;
	
	private Paper pape;
	private int papid;
	private int teaid;
	private String upname;
	private String dates;
	String suc;

	public String getUpname() {
		return upname;
	}

	public void setUpname(String upname) {
		this.upname = upname;
	}

	public File getFileCom() {
		return fileCom;
	}

	public void setFileCom(File fileCom) {
		this.fileCom = fileCom;
	}

	public String getFileComFileName() {
		return fileComFileName;
	}

	public void setFileComFileName(String fileComFileName) {
		this.fileComFileName = fileComFileName;
	}

	public String getSuc() {
		return suc;
	}

	public void setSuc(String suc) {
		this.suc = suc;
	}

	public int getTeaid() {
		return teaid;
	}

	public void setTeaid(int teaid) {
		this.teaid = teaid;
	}

	public String getDates() {
		return dates;
	}

	public void setDates(String dates) {
		this.dates = dates;
	}

	public Paper getPape() {
		return pape;
	}

	public void setPape(Paper pape) {
		this.pape = pape;
	}

	public int getPapid() {
		return papid;
	}

	public void setPapid(int papid) {
		this.papid = papid;
	}

	public String execute() throws Exception{
		
		if(fileCom!=null)
		{
			InputStream is = new FileInputStream(fileCom);

			uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");

			File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
	            FilePath=uploadPath+File.separator+this.getFileComFileName();

	     	    File toFile = new File(uploadPath, this.getFileComFileName());

	     		OutputStream os = new FileOutputStream(toFile);

	     		byte[] buffer = new byte[4096];

	     		int length = 0;

	     		while ((length = is.read(buffer)) > 0) {
	     			os.write(buffer, 0, length);
	     		}
	     		System.out.println("上传文件名"+fileComFileName);
	     		System.out.println(FilePath);

	     		is.close();

	     		os.close();
			
	     		pape.setUpload(fileComFileName);
		}else{
			pape.setUpload(upname);
		}
		
	
		
		pape.setPapId(papid);
		pape.setState(0);
		pape.setTeaId(teaid);
		Date d=new StringDate().StringToDate(dates);
		pape.setDate(d);
		
		if(pape.getMeetClass()==null){
			pape.setMeetClass(0);
		}
		if(pape.getMeetName()==null){
			pape.setMeetName("0");
		}
	    if(pape.getMeetPlace()==null){
	    	pape.setMeetPlace("0");
	    }
	    if(pape.getVolume()==null){
	    	pape.setVolume("0");
	    }
	    if(pape.getEsi()==null){
	    	pape.setEsi(0);
	    }
	    if(pape.getType()==null){
	    	pape.setType(0);
	    }
	
		if(new paper().updatePaper(pape, papid)){
			this.setSuc("success2");
			return SUCCESS;
		}
		return ERROR;
	}

}
